package com.botann.projects.utils;


import com.botann.projects.base.SGRuntimeException;
import com.botann.projects.constants.Code;
import org.springframework.util.StringUtils;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Locale;

/**
 * Created by liushanguo on 2017/5/25.
 */
public final class DateUtils {

    public static final String kDefaultFormat = "yyyy-MM-dd HH:mm:ss";
    public static final String kDefaultFormatStr = "yyyyMMddHHmmss";
    public static final String kDateFormat = "yyyy-MM-dd";
    public static final String kDayFormat = "yyyyMMdd";
    public static final String kTimeFormat = "HH:mm:ss";
    public static final String kMinuteFormat = "yyyy-MM-dd HH:mm";


    public static String nowDateStr(String format) {
        if (format == null) format = kDateFormat;
        return date2String(new Date(),format);
    }

    public static Date today() {
        return string2Date(nowDateStr(kDayFormat),kDayFormat);
    }

    public static Date yesterday() {
        return plusDay(-1,today());
    }
    public static Date yesterday(Date date) {
        return plusDay(-1,date);
    }

    public static Date tomorrow() {
        return plusDay(1,today());
    }
    public static Date tomorrow(Date date) {
        return plusDay(1,date);
    }
    public static Date nextDay(Date date) {
        return tomorrow(date);
    }


    /**
     * yyyy-MM-dd HH:mm:ss
     * yyyyMMddHHmmss:SSS
     */
    /**
     * Java将Unix时间戳转换成指定格式日期字符串
     * @param timestampString 秒时间戳 如："1473048265";
     * @param formats 要格式化的格式 默认："yyyy-MM-dd HH:mm:ss";
     *
     * @return 返回结果 如："2016-09-05 16:06:42";
     */
    public static String timeStamp2Date(String timestampString, String formats) {
        Long timestamp = Long.parseLong(timestampString) * 1000;
        return timeStamp2Date(timestamp,formats);
    }

    public static Date targetDate(String dateString, String formats) {
        DateFormat dateFormat = new SimpleDateFormat(formats);
        try {
           return dateFormat.parse(dateString);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static Date toDayEnd(Date date) {

        Calendar calendar = new GregorianCalendar();
        calendar.setTime(date);
        calendar.add(calendar.DATE,1);
        date = calendar.getTime();
        String dateStr = date2String(date,kDayFormat);
        date = string2Date(dateStr,kDayFormat);
        calendar.setTime(date);
        calendar.add(calendar.SECOND,-1);
        return calendar.getTime();
//        System.out.println("toDateEnd===========================================");
//        return  calendar.getTime();
    }
    public static Date toDayStart(Date date) {
        String dateStr = date2String(date,kDayFormat);
        return string2Date(dateStr,kDayFormat);
//        Calendar calendar = new GregorianCalendar();
//        calendar.setTime(date);
//        calendar.add(calendar.DATE,-1);
//        calendar.add(calendar.DATE,1);
////        calendar.add(calendar.SECOND,-1);
//        return  calendar.getTime();
    }

    public static Date string2Date(String dateStr, String formats) {
        if (dateStr == null) return null;
        SimpleDateFormat sdf = new SimpleDateFormat(formats);
        Date date = null;
        try {
            date = sdf.parse(dateStr);
        } catch (ParseException pe) {
            throw new SGRuntimeException(Code.PARAMETER_WRONG,"时间区间错误!");
        }
       return date;
    }

    /**
     * Java将Unix时间戳转换成指定格式日期字符串
     * @param timestamp 毫秒时间戳
     * @return String
     */
    public static String timeStamp2Date(Long timestamp, String format) {
        if (StringUtils.isEmpty(format)) format = kDefaultFormat;
        return date2String(new Date(timestamp), format);
    }
    public static String date2String (Date date) {
        return date2String(date,kDateFormat);
    }

    public static String date2String (Date date, String format) {
        if (date==null){ return "";}
        String dateStr = new SimpleDateFormat(format, Locale.CHINA).format(date);
        return dateStr;
    }


    public static String timeSerial(long millis,String format){
        return timeStamp2Date(millis,format);
    }
    public static String timeSerial(long millis){
        return timeSerial(millis, "yyyyMMddHHmmss");
    }
    public static String timeSerial(){
        return timeSerial(System.currentTimeMillis());
    }


    /**
     * 根据年 月 获取对应的月份 天数
     * */
    public static int getDaysByYearMonth(int year, int month) {

        Calendar a = Calendar.getInstance();
        a.set(Calendar.YEAR, year);
        a.set(Calendar.MONTH, month - 1);
        a.set(Calendar.DATE, 1);
        a.roll(Calendar.DATE, -1);
        int maxDate = a.get(Calendar.DATE);
        return maxDate;
    }

    /**
     * 获取时间,开始结束时间
     * @param timeSpace String not Null
     * @return Date []
     */
    public static Date [] startEndDatesWithTimeSpace(String timeSpace) {
        if (StringUtils.isEmpty(timeSpace)) return null;
        if (!timeSpace.contains("~")) return null;
        String [] dateStrs = timeSpace.split("~");
        Date startDate = DateUtils.string2Date(dateStrs[0]+" 00:00:00",kDefaultFormat);
        Date endDate = DateUtils.string2Date(dateStrs[1]+" 23:59:59",kDefaultFormat);
        return new Date[]{startDate,endDate};
    }

    /**
     * 指定日期加上天数后的日期
     * @param num 为增加的天数
     * @param date 创建时间
     * @return
     * @throws ParseException
     */
    public static Date plusDay(int num,Date date) {

        SimpleDateFormat format = new SimpleDateFormat(kDefaultFormat);
        Calendar ca = Calendar.getInstance();
        ca.setTime(date);
        ca.add(Calendar.DATE, num);
        date = ca.getTime();
        return date;
    }



}
